package com.zwb.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwb.boot.entity.ShoppingCart;
import com.zwb.boot.mapper.ShoppingCartMapper;
import com.zwb.boot.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 头发又黑又长
 * @Date 2022/7/26 10:28
 * @email zwb15083976291@163.com
 */
@Slf4j
@Service
@Transactional
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
        implements ShoppingCartService {


    /**
     * 通过 userId 和 DishId/SetmealId 查询数据
     *
     * @param shoppingCart
     * @return ShoppingCart
     */
    @Override
    public ShoppingCart findByUserIdWithDishIdOrSetmealId(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId())
                // 如果 菜品id 存在
                .eq(ObjectUtils.isNotEmpty(shoppingCart.getDishId()),
                        ShoppingCart::getDishId, shoppingCart.getDishId())
                // 如果套餐id存在
                .eq(ObjectUtils.isNotEmpty(shoppingCart.getSetmealId()),
                        ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        return this.getOne(queryWrapper);
    }

    /**
     * 根据 用户id查询购物车数据
     *
     * @param id
     * @return List<ShoppingCart>
     */
    @Override
    public List<ShoppingCart> listByUserId(Long id) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, id);
        return this.list(queryWrapper);
    }
}
